import concurrent.futures
import threading
import time


class MyLockedClass:
    def __init__(self):
        self.lock = threading.Lock()  # 每个实例都有自己的锁

    def locked_wrap_method(self):
        self.locked_method()
        time.sleep(2)
        print("abc\n")

    def locked_method(self):
        with self.lock:  # 使用with语句确保锁会自动释放
            # 执行需要锁定的操作
            print(f"{threading.current_thread().name} is executing locked_method on instance {id(self)}")


# 创建多个实例
instances = [MyLockedClass() for _ in range(5)]

# for instance in instances:
#    instance.locked_wrap_method()
#    print("def")


def worker(instance):
    instance.locked_wrap_method()
    return 'aaaa'


# 使用线程池来并行执行方法
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
    futures = [executor.submit(worker, instance) for instance in instances]
    print("\n123123123")
    #for future in concurrent.futures.as_completed(futures):
    #    print(future.result() + "\n")
    #print("xxcvxcv")

print("yyyyyyy")